﻿
@{
    Layout = null;
}

@using System.Data;

<!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登錄</title>
    <link href="~/Content/LoginStyle.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-3.4.1.min.js" ></script>
</head>
<body>
    <div class="htmleaf-container">
        <div class="wrapper">
            <div class="container">
                <h1>黃石簡易平台</h1>
                <form class="form" method="post">
                    <input id="txtUserName" type="text" placeholder="請輸入用戶名" onkeydown="login(event, 'txt')" />
                    <input id="txtPassWord" type="password" placeholder="請輸入密碼" onkeydown="login(event, 'txt')" />
                    <select id="selPage">
                        <option value="00">請選擇主頁</option>
                        @{
                            foreach (DataRow item in ViewBag.PageInfo)
                            {
                                <option value="@item["PAGE_URL2"].ToString()">@item["PAGE_NAME"].ToString()</option>
                            }
                        }
                    </select>
                    <input id="btnLogin" type="button" value="登錄" onclick="login(event, 'btn')" />
                </form>
            </div>

            <ul class="bg-bubbles">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>
    <div style="text-align: center; margin: 50px 0; font: normal 14px/24px 'MicroSoft YaHei'; color: #000000">
    </div>
</body>
</html>

<script>
    function login(event, type) {
        if (type == "btn" || (type == "txt" && event.key == "Enter")) {
            var un = $("#txtUserName").val();
            var pw = $("#txtPassWord").val();
            if (un == "" || un == null) {
                alert("請輸入用戶名！");
                return;
            }
            if (pw == "" || pw == null) {
                alert("請輸入密碼！");
                return;
            }
            $.ajax({
                type: "post",
                //url: "/Login/LoginOn",
                url: "/Login/Validate",
                data: {
                    userName: un,
                    passWord: pw,
                    page: $("#selPage").val()
                },
                success: function (data) {
                    var result = JSON.parse(data);
                    if (result.flag) {
                        if (result.pwdDue) {
                            alert(result.msg);
                        }
                        window.location.href = "/" + result.pageindex;
                    } else {
                        alert(result.msg);
                        if (result.updatePwd) {
                            window.location.href = "@Url.Action("UpdatePwd")";
                            return;
                        }
                        if (result.reset) {
                            if (confirm("檢測到賬號已鎖定，是否重置密碼？")) {
                                $.ajax({
                                    type: "post",
                                    url: "/Login/ResetPwd",
                                    data: {
                                        userName: un
                                    },
                                    success: function (data) {
                                        var result = JSON.parse(data);
                                        alert(result.msg);
                                    },
                                    error: function (err) {

                                    }
                                });
                            }
                        }
                        window.location.reload();
                    }
                },
                error: function (err) {

                }
            });
        }
    }
</script>